/*
 * FiltrarAtendimentos.java
 *
 * Created on 19/06/2011, 12:56:03
 */

package pbf.dialog;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Atendimento;
import pbf.conf.hibernate.entidades.Familia;
import pbf.util.DataHora;
import pbf.util.Mensagem;

/**
 *
 * @author Ricardo
 */
public class FiltrarAtendimentos extends javax.swing.JDialog {

    private Familia familia = null;
    private ArrayList<Atendimento> listaAtendimento = new ArrayList<Atendimento>();
    private static Object[] objetos = new Object[3];
    private static Session sessao = sessao = HibernateUtil.openSession();
    private DetalhesAtendimento detalhesAtendimento = new DetalhesAtendimento(null , true);

    public FiltrarAtendimentos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private void setFormulario(){
        familiaNome.setText(familia.getFamiliaNome());
        familiaCpf.setText(familia.getFamiliaCpf());
        setTable();
    }

    private void setTable()
    {
        DefaultTableModel model = (DefaultTableModel) tabela.getModel();
        model.setNumRows(0);

        sessao.beginTransaction();

        listaAtendimento = (ArrayList<Atendimento>) sessao.createSQLQuery("SELECT ATENDIMENTO.* FROM atendimento,agendamento WHERE AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND AGENDAMENTO_FK_FAMILIA="+familia.getFamiliaId()).addEntity(Atendimento.class).list();

        for(Atendimento f : listaAtendimento)
        {
            objetos[0] = DataHora.getData(f.getAtendimentoDtAtendimento());
            objetos[1] = f.getStatusAtendimento().getStStatus();
            objetos[2] = f.getAtendimentoComentario();

            model.addRow(objetos);
        }
        sessao.getTransaction().commit();
    }

    private void setFiltrar(){
        DefaultTableModel model = (DefaultTableModel) tabela.getModel();
        model.setNumRows(0);

        sessao = sessao = HibernateUtil.openSession();
        sessao.beginTransaction();

        for(Atendimento f : listaAtendimento)
        {
            objetos[0] = DataHora.getData(f.getAtendimentoDtAtendimento());
            objetos[1] = f.getStatusAtendimento().getStStatus();
            objetos[2] = f.getAtendimentoComentario();

            model.addRow(objetos);
        }
        sessao.getTransaction().commit();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator9 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        familiaCpf = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        familiaNome = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(null);
        getContentPane().add(jSeparator9);
        jSeparator9.setBounds(20, 40, 450, 10);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setText("Filtrar Atendimento da Família");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(20, 20, 290, 20);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel16.setText("CPF:");
        getContentPane().add(jLabel16);
        jLabel16.setBounds(20, 80, 50, 20);

        familiaCpf.setEditable(false);
        familiaCpf.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(familiaCpf);
        familiaCpf.setBounds(190, 80, 120, 23);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/cross-circle.png"))); // NOI18N
        jButton1.setText("Fechar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(370, 370, 100, 25);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel18.setText("Nome do Resposável:");
        getContentPane().add(jLabel18);
        jLabel18.setBounds(20, 50, 170, 20);

        familiaNome.setEditable(false);
        familiaNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(familiaNome);
        familiaNome.setBounds(190, 50, 280, 23);

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Data", "Status", "Comentário"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(80);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(300);
        tabela.getColumnModel().getColumn(2).setPreferredWidth(100);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 120, 452, 210);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 360, 500, 10);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/clipboard-invoice.png"))); // NOI18N
        jButton2.setText("Flitrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(260, 370, 100, 25);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/search.png"))); // NOI18N
        jButton3.setText("Procurar Família");
        jButton3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(320, 80, 150, 25);

        jLabel1.setText("Clique duas vezes na linha para vê os detalhes do Atendimento da Família.");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 330, 450, 14);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-510)/2, (screenSize.height-441)/2, 510, 441);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        ProcurarFamilia procurarFamilia = new ProcurarFamilia(null, true);
        familia = procurarFamilia.getFamilia();

        if (familia != null) {
            setFormulario();
        } else {
            Mensagem.mensageiroAtencao("Nenhuma Família Encontrada");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        if (evt.getClickCount() == 2)
        {
                int linha = tabela.getSelectedRow();
                detalhesAtendimento.setFormulario(listaAtendimento.get(linha), listaAtendimento.get(linha).getAgendamento().getDescricaoDoAtendimento(), listaAtendimento.get(linha).getStatusAtendimento());
        }
    }//GEN-LAST:event_tabelaMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (familia == null) {
            Mensagem.mensageiroAtencao("Para filtrar é necessario procurar uma família primeiro!");
        } else {
            Filtrar filtrar = new Filtrar(null, true);
            listaAtendimento = filtrar.getAtendimentos(familia.getFamiliaId());
            setFiltrar();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FiltrarAtendimentos dialog = new FiltrarAtendimentos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField familiaCpf;
    private javax.swing.JTextField familiaNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables
}
